<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="root">
        <div class="box">
            <h2>h2</h2>
            <ul>
                <li>li1</li>
                <li>li2</li>
            </ul>
        </div>
    </div>

    <script>
    let arr = new Proxy([12,5,8],{
        set(arr,name,val){
            arr[name] = val;
            render();
            return true;// 为了操作继续
        }
    })

    let root = document.querySelector('#root');
    let box = document.querySelector('.box');
    let frag = document.createDocumentFragment();
    frag.appendChild(box);

    // 使用异步操作，避免多次渲染方法
    let timer = null;
    function render(){
        clearTimeout(timer);// 每次需要清空
        timer = setTimeout(function(){
            console.log('render');
            root.innerHTML = '';
            arr.forEach(item=>{
                let el = frag.cloneNode(true);
                el.children[0].getElementsByTagName('h2')[0].innerHTML = item;
                root.appendChild(el);
            })
        },0);
    }
    render();
    </script>
</body>
</html>